package com.wolves.career.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wolves.career.entity.JobOpportunityRequire;
import com.wolves.career.service.JobOpportunityRequireService;
import com.wolves.career.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.management.Query;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 麦翔盛
 * @since 2021-10-23
 * 职位和技术组合的表  ----  需求控制类
 */
@RestController
@RequestMapping("/job/require")
@CrossOrigin
public class JobOpportunityRequireController {

    @Autowired
    private JobOpportunityRequireService ORservice;

    //根据需求id获取需求

    /**
     * 根据需求id获取需求
     *
     * @param requireId
     * @return
     */
    @GetMapping("/getRequireById/{requireId}")
    public R getRequireById(@PathVariable String requireId) {
        JobOpportunityRequire OR = ORservice.getById(requireId);
        return R.ok().data("OR", OR);
    }

    //根据技术id获取需求

    /**
     * 根据技术id分查询需求
     *
     * @param positionId 职位id
     * @param pageNo     页码
     * @param limit      每页限制数量
     * @return
     */
    @GetMapping("/getRequireByTechnology/{pageNo}/{limit}/{positionId}")
    public R getRequireByTechnology(@PathVariable String positionId,
                                    @PathVariable Long pageNo,
                                    @PathVariable Long limit) {
        QueryWrapper<JobOpportunityRequire> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("technology_id", positionId);
        Page<JobOpportunityRequire> page = new Page<>();

        IPage<JobOpportunityRequire> ORpage = ORservice.page(page, queryWrapper);

        List<JobOpportunityRequire> records = ORpage.getRecords();
        long total = ORpage.getTotal();

        return R.ok().data("records", records).data("total", total);

    }

    //根据职位获取需求

    /**
     * 格努职位id获取需求
     *
     * @param pageNo     当前页码
     * @param limit      每页限制条数
     * @param positionId 职位id
     * @return
     */
    @GetMapping("/getRequireByPosition/{pageNo}/{limit}/{positionId}")
    public R getRequireByPosition(@PathVariable Long pageNo,
                                  @PathVariable Long limit,
                                  @PathVariable String positionId) {
        QueryWrapper<JobOpportunityRequire> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("position_id", positionId);
        Page<JobOpportunityRequire> page = new Page<>();

        IPage<JobOpportunityRequire> ORpage = ORservice.page(page, queryWrapper);

        List<JobOpportunityRequire> records = ORpage.getRecords();
        long total = ORpage.getTotal();

        return R.ok().data("records", records).data("total", total);

    }

    /**
     * 更新需求
     * @param jobOpportunityRequire 需求类
     * @return
     */
    @PutMapping("/updateRequire")
    public R updateRequire(@RequestBody(required = true)
                                       JobOpportunityRequire jobOpportunityRequire) {

        boolean update = ORservice.updateById(jobOpportunityRequire);
        if (!update){
            return R.error().message("更新失败").data("success",false);
        }
        return R.ok().data("success",true);

    }

    /**
     * 根据id删除需求
     * @param requireId
     * @return
     */
    @DeleteMapping("/removeRequire/{requireId}")
    public R removeRequire(@PathVariable String requireId) {
        boolean remove = ORservice.removeById(requireId);
        if (!remove){
            return R.error().message("更新失败").data("success",false);
        }
        return R.ok().data("success",true);
    }

    /**
     * 添加职位，因为前端传过来对的数据不可能是数据库库不存在额
     * 所以这里不做是否存在判断，如果后期处于安全考虑优化才将这里优化
     * 虽然打这段文字的时间我已经可能已经优化好了
     * @param jobOpportunityRequire 需求类
     * @return
     */
    public R addRequire(@RequestBody(required = true) JobOpportunityRequire jobOpportunityRequire) {
        boolean save = ORservice.save(jobOpportunityRequire);
        if (!save){
            return R.error().message("插入失败");
        }
        return R.ok().data("success",true);
    }


}

